const useManualFetch = (props: UseManualFetchProps) => {
  const { api, method, params } = props;
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);

  // TODO 请在这里补充你的代码
  const send = async () => {
    setLoading(true);
    setError(null);

    try {
      let response;
      if (method === 'get') {
        response = await fetch(api);
      } else if (method === 'post') {
        response = await fetch(api, {
          method: 'post',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(params),
        });
      }
      if (!response.ok) {
        throw new Error('error');
      }

      const responseData = await response.json();
      setData(responseData);
    } catch (err) {
      setError(err.message);
    } finally {
      setLoading(false);
    }
  };
  return [data, loading, error, send];
};
